home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_dates2.arc / SEPARATE.ARC / JTOG.C < prev    next >
Text File  |  1991-01-10  |  4KB  |  99 lines

  1. /************************************************************************/
  2. /*                *** jtog.c ***                */
  3. /*                                    */
  4. /* This function converts a julian day number ( 1 = 1st day AD ), into    */
  5. /* either a gregorian date character string, in any one of the four    */
  6. /* formats described below, or an array of three integers representing    */
  7. /* the month, day, and year, respectively, of the equivalent gregorian    */
  8. /* date.  Storage for the output is provided by the calling routine.    */
  9. /*                                    */
  10. /* The input values, in their proper order, are:            */
  11. /*                                    */
  12. /*  1.  the julian date to be converted,          ( unsigned long )    */
  13. /*                                    */
  14. /*  2.  a pointer to the output array,              ( char * )    */
  15. /*                                    */
  16. /*  3.  the format code ( described below )                */
  17. /*    specifying the desired output format.          ( unsigned int )    */
  18. /*                                    */
  19. /* The format codes are defined as follows:                */
  20. /*                                    */
  21. /*   CODE            - EXPLANATION -                */
  22. /*                                    */
  23. /*     0  -  output array will hold three integers defined as:        */
  24. /*                                    */
  25. /*            array[0] = month    (   mm )        */
  26. /*            array[1] = day        (   dd )        */
  27. /*            array[2] = year        ( yyyy )        */
  28. /*                                    */
  29. /*         If this format option is used, a type cast will have to    */
  30. /*         be performed on the output pointer to convert it to the    */
  31. /*         appropriate pointer type. ( see example below )        */
  32. /*                                    */
  33. /*     1  -  output string will hold a character string in the        */
  34. /*         format "mmddyy"      ( minimum - char string[7] )        */
  35. /*                                    */
  36. /*     2  -  output string will hold a character string in the        */
  37. /*         format "mmddyyyy"      ( minimum - char string[9] )        */
  38. /*                                    */
  39. /*     3  -  output string will hold a character string in the        */
  40. /*         format "mm/dd/yy"      ( minimum - char string[9] )        */
  41. /*                                    */
  42. /*     4  -  output string will hold a character string in the        */
  43. /*         format "mm/dd/yyyy"  ( minimum - char string[11] )        */
  44. /*                                    */
  45. /*   NOTE -  input of any format code not listed above will be        */
  46. /*         interpreted as code 0.                    */
  47. /*                                    */
  48. /*   The function returns a character pointer to the output array.    */
  49. /*    ______________________________________________________________    */
  50. /*   |           *** example of integer array output ***        |    */
  51. /*   |                                    |    */
  52. /*   |  int    month, dte_array[3];                    |    */
  53. /*   |  long    julian = 548784;                    |    */
  54. /*   |                                    |    */
  55. /*   |  month = ( (int *)jtog( julian, (char *)dte_array, 0 ) )[0]; |    */
  56. /*   |______________________________________________________________|    */
  57. /*                                    */
  58. /*   WARNING -  the output array pointed to must be dimensioned        */
  59. /*        accordingly or a memory overwrite will occur.        */
  60. /************************************************************************/
  61.  
  62. char *jtog( julian, date, fmt )
  63.  
  64. long julian;
  65. char *date;
  66. unsigned int fmt;
  67.  
  68. {
  69.     int  month, day, year, i;
  70.     long numdte, indx = !( fmt & 1 ) * 9900 + 100;
  71.  
  72.     year    = ( julian += 146037 ) * 400 / 146097;
  73.     julian -= (long)year*365 + year/4 - year/100 + year/400;
  74.     year   += julian / ( i = 366 - !!( ( year + 1 ) % 4 ) );
  75.  
  76.     if ( !( julian %= i ) ) julian = 365 + !( year % 4 ), --year;
  77.  
  78.     year += ( month = ( day = (int)julian * 5 - 3 ) / 153 + 2 ) / 12 - 400;
  79.     month = month % 12 + 1;
  80.     day   = day % 153 / 5 + 1;
  81.  
  82.     if ( fmt && fmt < 5 )
  83.     {
  84.          numdte = (long)month * 100 * indx + day * indx + year % indx;
  85.          for ( indx *= 10000, i = 0; indx > 1; ++i )
  86.            date[i] = ( fmt > 2 && ( i == 2 || i == 5 ) )  ?  '/'
  87.                : (char)( numdte / ( indx /= 10 ) % 10 + 48 );
  88.          date[i] = '\0';
  89.     }
  90.     else
  91.     {
  92.          ((int *)date)[0] = month;
  93.          ((int *)date)[1] = day;
  94.          ((int *)date)[2] = year;
  95.     }
  96.  
  97.     return( date );
  98. }
  99.